#include "ScnsDs18b20.h"

#if defined(SCNS_DS18B20_ENABLE)&&SCNS_DS18B20_ENABLE==1

#include "stm32f7xx_hal.h"
#include "ScnsMenu.h"
#include "ScnsMath.h"
#include "ScnsSmctpExtOscilloscope.h"

const uint8 scnsBspDs18b20Id[SCNS_DS18B20_MAX][8]={
    [SCNS_DS18B20_1]={0X28,0XFF,0XCF,0XAB,0X60,0X16,0X03,0X39},
};

ScnsStatus scnsBspDs18b20PostInit(ScnsDs18b20Enum ds1N)
{
    scnsMenuSetPage(3,"ds18b20",NULL);
    scnsMenuAddConstStruct(3,scnsDs18b20Result[ds1N],temperature,,SCNS_TYPE_FLOAT);
    return SCNS_STATUS_OK;
}

void scnsBspDs18b20SetDir(ScnsDs18b20Enum ds1N,uint8 in)
{
    const uint32 hb=scnsMathGetHighest1(ONE_WIRE_Pin)*2;
    if(in)
    {
        ONE_WIRE_GPIO_Port->MODER&=~(3<<(hb));
        ONE_WIRE_GPIO_Port->MODER|=0<<(hb);
    }
    else
    {
        ONE_WIRE_GPIO_Port->MODER&=~(3<<(hb));
        ONE_WIRE_GPIO_Port->MODER|=1<<(hb);
    }
}

void scnsBspDs18b20Set(ScnsDs18b20Enum ds1N,uint8 dat)
{
    ONE_WIRE_GPIO_Port->BSRR=(dat)?(ONE_WIRE_Pin):(ONE_WIRE_Pin<<16);
}

uint8 scnsBspDs18b20Get(ScnsDs18b20Enum ds1N)
{
    return (ONE_WIRE_GPIO_Port->IDR&ONE_WIRE_Pin)?1:0;
}

void scnsBspDs18b20ResultUpdate(ScnsDs18b20Enum ds1N)
{
    if(ds1N==SCNS_DS18B20_1&&scnsMenuCanPageSend(3))
    {
        scnsSmctpExtOscilloscopeSendFloat(SCNS_SMCTP_CHANNEL_VISUAL_SCOPE,0,scnsDs18b20Result[ds1N].temperature);
    }
}

#endif
